
Imports System.Collections.ObjectModel
Imports System.Data.Common
Imports System.Collections.Generic
Imports System.Runtime.InteropServices
Imports System.Data.SqlClient
Imports System.Data
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports BusinessEntities

Public Class ConvocatoriaDA
    Inherits Base
    Implements iConvocatoriaDAO

    Public Function ObtenerConvocatoriaActivas() As DataTable Implements iConvocatoriaDAO.ObtenerConvocatoriaActivas
        Try
            Using command As DbCommand = Me.DataBase.GetStoredProcCommand("pa_Convocatoria_Activa")
                Using ds As DataSet = Me.DataBase.ExecuteDataSet(command)
                    If ds.Tables.Count.Equals(0) Then
                        Return Nothing
                    Else
                        Return ds.Tables(0)
                    End If
                End Using
            End Using
        Catch ex As Exception
            Throw
        End Try
    End Function

    Public Function ObtenerPreSeleccionadoPorConvocatoria(intConvocatoria As Integer) As DataTable Implements iConvocatoriaDAO.ObtenerPreSeleccionadoPorConvocatoria
        Try
            Using command As DbCommand = Me.DataBase.GetStoredProcCommand("pa_Convocatoria_PreSeleccionado")
                Me.DataBase.AddInParameter(command, "@Convocatoria", DbType.Int32, intConvocatoria)

                Using ds As DataSet = Me.DataBase.ExecuteDataSet(command)
                    If ds.Tables.Count.Equals(0) Then
                        Return Nothing
                    Else
                        Return ds.Tables(0)
                    End If
                End Using
            End Using
        Catch ex As Exception
            Throw
        End Try
    End Function

    Public Function ObtenerConvocatoria(intConvocatoria As Integer) As ConvocatoriaBE Implements iConvocatoriaDAO.ObtenerConvocatoria
        Try
            Dim objBE As ConvocatoriaBE = Nothing

            Using command As DbCommand = Me.DataBase.GetStoredProcCommand("pa_Convocatoria_select")
                Me.DataBase.AddInParameter(command, "@Convocatoria", DbType.Int32, intConvocatoria)

                Using reader As IDataReader = Me.DataBase.ExecuteReader(command)
                    objBE = Me.ReadItem(reader)
                End Using
            End Using

            Return objBE
        Catch ex As Exception
            Throw
        End Try
    End Function

    Private Function ReadItem(ByVal reader As IDataReader) As ConvocatoriaBE Implements iConvocatoriaDAO.ReadItem
        Try
            Dim objBE As New ConvocatoriaBE

            While (reader.Read())
                If Convert.IsDBNull(reader("IdConvocatoria")) Then
                    objBE.ID = 0
                Else
                    objBE.ID = CInt(reader("IdConvocatoria"))
                End If

                If Convert.IsDBNull(reader("nombre")) Then
                    objBE.Nombre = String.Empty
                Else
                    objBE.Nombre = CStr(reader("nombre"))
                End If

                If Convert.IsDBNull(reader("codigo")) Then
                    objBE.Codigo = String.Empty
                Else
                    objBE.Codigo = CStr(reader("codigo"))
                End If

                If Convert.IsDBNull(reader("fechacreacion")) Then
                    objBE.FechaCreacion = Nothing
                Else
                    objBE.FechaCreacion = CDate(reader("fechacreacion"))
                End If

                If Convert.IsDBNull(reader("requerimiento")) Then
                    objBE.Requerimiento = String.Empty
                Else
                    objBE.Requerimiento = CStr(reader("requerimiento"))
                End If

                If Convert.IsDBNull(reader("fechafin")) Then
                    objBE.FechaFin = Nothing
                Else
                    objBE.FechaFin = CDate(reader("fechafin"))
                End If

                If Convert.IsDBNull(reader("visita")) Then
                    objBE.Visita = False
                Else
                    objBE.Visita = CBool(reader("visita"))
                End If
            End While

            Return objBE
        Catch ex As Exception
            Throw
        End Try
    End Function



End Class
